<script setup lang='ts'>
  import { onMounted, reactive, ref } from 'vue';
  import { gamesApi } from '@/api/controller';
  import { ToastSuccess, ToastWarning, Toastloading, copyText, $router } from '@/utils/public';
  import { ElMessageBox } from 'element-plus';
  import xmDatePicker from '@/components/xmDatePicker/index.vue';
  import Pagination from "@/components/Pagination/index.vue";
  import activityForm from './activityForm.vue';
  import { useRouter } from 'vue-router'
  const activityFormRef = ref();
  const action = reactive({
    total:0,
    dialogTableVisible:false,
    activityID:0,
    list:[] as any[],
    timeObj:{
      playTime:0,
      endTime:0,
    }
  })
  const typeOptions = [
    {type:1,text:'用户ID'},
    {type:2,text:'用户手机号'},
  ]
  const listQuery = reactive({
    page:1,
    limit:20,
    custom_id:'',
    game_name:''
  })
  const handleSerach = ()=>{
    listQuery.page = 1
    getList()
  }
  const handleForm = (row:any = {})=>{
    activityFormRef.value.openDialog({
      ...row,
      typeOptions
    })
  }
  const openComponents = (row:any = {},path:any)=>{
    const { id } = row
    $router().push({
      path,
      query: {
        activityID:id
      }
    })
  }
  const getType = (type:number = 0)=>{
    return [
      {type:'primary',text:type},
      {type:'success',text:'开启'},
      {type:'danger',text:'未开启'},
    ][type] || {type:'',text:type}
  }
  const handleEmpty = (row:any = {})=>{
    const { id=0, game_name='' } = row
    if(!id) return ToastWarning('缺少id')
    ElMessageBox.confirm(`确定清除活动：${game_name}的数据吗?`, "提示", {
      confirmButtonText: "确定",
      cancelButtonText: "取消",
      type: "warning",
    }).then(async () => {
      const loading = Toastloading();
      gamesApi.customClear({custom_id:id}).then(()=>{
        ToastSuccess("清除成功!");
        loading.close();
        getList();
      }).catch(()=>{
        loading.close();
      })
    }).catch(() => {});
  }
  const getList = async()=> {
    const loading = Toastloading()
    const { playTime:date_st, endTime:date_ed } = action.timeObj
    const res = await gamesApi.customList({
      date_st,
      date_ed,
      ...listQuery
    })
    loading.close()
    const { data=[], total=0 } = res.data
    action.total = total
    action.list = data.map((res:any)=>{
      const { id=0 } = res
      return {
        activity_url:`kkaudio://web?url=${import.meta.env.VITE_OSS_ACTIVITY_HTTP}/activity/templ/index.html?custom_id=${id}`,
        ...res
      }
    })
  }
  onMounted(()=>{
    getList()
  })
</script>

<template>
  <div class="xm-content">
    <div class="xm-headTab">
      <div class="labelBox">
        <div class="labelItem">
          <div class="label">活动ID：</div>
          <el-input type="text" v-model="listQuery.custom_id" placeholder="请输入活动ID" />
        </div>
        <div class="labelItem">
          <div class="label">活动名称：</div>
          <el-input type="text" v-model="listQuery.game_name" placeholder="请输入活动名称" />
        </div>
        <xmDatePicker type="daterange" v-model:timeval="action.timeObj" />
        <div class="labelItem">
          <el-button type="primary" @click="handleSerach">查询</el-button>
          <el-button v-rule="'stencils_customAdd'" type="primary" @click="handleForm()">创建活动</el-button>
        </div>
      </div>
    </div>
    <el-table
      class="xm-main xm-TableCenter"
      :data="action.list"
      border
    >
      <el-table-column fixed="left" width="100" prop="id" label="活动ID" />
      <el-table-column fixed="left" min-width="180" show-overflow-tooltip prop="game_name" label="活动名称" />
      <el-table-column width="180" prop="start_time" label="开始时间" />
      <el-table-column width="180" prop="end_time" label="结束时间" />
      <el-table-column width="180" prop="online_time" label="正式上线时间" />
      <el-table-column width="160" prop="if_rank" label="榜单">
        <template #default="scoped">
          <el-tag
            :type="getType(scoped.row.if_rank)?.type"
          >
          状态：{{ getType(scoped.row.if_rank)?.text }}
          <span class="pointer" @click="openComponents(scoped.row,'/stencil/rankingList')">点击编辑</span>
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column width="160" prop="if_build" label="盖楼">
        <template #default="scoped">
          <el-tag
            :type="getType(scoped.row.if_build)?.type"
          >
          状态：{{ getType(scoped.row.if_build)?.text }}
          <span class="pointer" @click="openComponents(scoped.row,'/stencil/floor')">点击编辑</span>
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column width="160" prop="if_lyb" label="留言板">
        <template #default="scoped">
          <el-tag
            :type="getType(scoped.row.if_lyb)?.type"
          >
          状态：{{ getType(scoped.row.if_lyb)?.text }}
          <span class="pointer" @click="openComponents(scoped.row,'/stencil/message')">点击编辑</span>
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column width="160" prop="if_recharge" label="充值返现">
        <template #default="scoped">
          <el-tag
            :type="getType(scoped.row.if_recharge)?.type"
          >
          状态：{{ getType(scoped.row.if_recharge)?.text }}
          <span class="pointer" @click="openComponents(scoped.row,'/stencil/rankingList')">点击编辑</span>
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column width="120" prop="rank_gift_ids" label="榜单礼物ID">
        <template #default="scoped">
          {{ scoped.row.rank_gift_ids == 'all' ? '全部礼物' : scoped.row.rank_gift_ids }}
        </template>
      </el-table-column>
      <el-table-column width="120" prop="wealth_gift_id" label="富豪值礼物ID" />
      <el-table-column width="220" show-overflow-tooltip prop="css" label="样式文件名称" />
      <el-table-column width="220" show-overflow-tooltip prop="days" label="活动链接(点击复制)">
        <template #default="scoped">
          <span class="pointer" @click="copyText(scoped.row.activity_url)" > {{ scoped.row.activity_url }} </span>
        </template>
      </el-table-column>
      <el-table-column width="180" prop="created_at" label="创建时间" />
      <el-table-column width="220" fixed="right" label="操作">
        <template #default="scoped">
          <el-button type="warning" v-rule="'stencil_customClear'" @click="handleEmpty(scoped.row)">清空数据</el-button>
          <el-button type="primary" v-rule="'stencils-customEdit'" @click="handleForm(scoped.row)">编辑</el-button>
        </template>
      </el-table-column>
    </el-table>
    <Pagination v-show="action.total > 0" :total="action.total" v-model:page="listQuery.page"
      v-model:limit="listQuery.limit" @pagination="getList" />
    <activityForm ref="activityFormRef" @update-list="getList" />
  </div>
  
</template>